using System;
using System.Text;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Web;

using Framework;

namespace SchemaMonitor
{
    //Collection Class (Customisable half)
    public partial class CLabelList
    {
		#region Filters
		#endregion

		#region Aggregation
		public CPhotoHashList Photos { get { return PhotoHashs; } } 
		#endregion


		#region Members
		private CMetaTagList _metaTags;
		private List<int> _metaTagIds;
		#endregion

		#region Resolve Associative table (and sort)
		public CMetaTagList MetaTags
		{
			get
			{
				if (_metaTags == null || this.Count != _metaTags.Count)
				{
					lock (this)
					{
						if (null == _metaTags)
						{
							_metaTags = new CMetaTagList(this.Count);
							foreach (CLabel i in this)
								if (i.MetaTag != null)
									_metaTags.Add(i.MetaTag);
							_metaTags.Sort();
						}
					}
				}
				return _metaTags;
			}
		}
		public CMetaTagList RemainingMetaTags(string search)
		{
			CMetaTagList temp = new CMetaTagList(CMetaTag.Cache.Search(search));
			temp.Remove(this.MetaTags);
			return temp;
		}
		#endregion

		#region Resolve/Isolate PKs
		public List<int> MetaTagIds
		{
			get
			{
				if (null == _metaTagIds)
				{
					lock (this)
					{
						if (null == _metaTagIds)
						{
							_metaTagIds = new List<int>(this.Count);
							foreach (CLabel i in this)
								_metaTagIds.Add(i.LabelTagId);
						}
					}
				}
				return _metaTagIds;
			}
		}
		#endregion


		#region Members
		private CPhotoHashList _photoHashs;
		private List<Guid> _photoHashIds;
		#endregion

		#region Resolve Associative table (and sort)
		public CPhotoHashList PhotoHashs
		{
			get
			{
				if (_photoHashs == null)
				{
					lock (this)
					{
						if (null == _photoHashs || this.Count != _photoHashs.Count)
						{
							_photoHashs = new CPhotoHashList(this.Count);
							foreach (CLabel i in this)
								if (i.PhotoHash != null)
									_photoHashs.Add(i.PhotoHash);
							_photoHashs.Sort();
						}
					}
				}
				return _photoHashs;
			}
		}
		public CPhotoHashList RemainingPhotoHashs(string search)
		{
			CPhotoHashList temp = new CPhotoHashList(CPhotoHash.Cache.Search(search));
			temp.Remove(this.PhotoHashs);
			return temp;
		}
		#endregion

		#region Resolve/Isolate PKs
		public List<Guid> PhotoHashIds
		{
			get
			{
				if (null == _photoHashIds)
				{
					lock (this)
					{
						if (null == _photoHashIds)
						{
							_photoHashIds = new List<Guid>(this.Count);
							foreach (CLabel i in this)
								_photoHashIds.Add(i.LabelGuid);
						}
					}
				}
				return _photoHashIds;
			}
		}
		#endregion



		#region Cloning
		public CLabelList Clone(CDataSrc target) //, int parentId)
        {
            //No Transaction
            if (target is CDataSrcRemote)
                return Clone(target, null); //, parentId);

            //Transaction
            using (IDbConnection cn = target.Local.Connection())
            {
                IDbTransaction tx = cn.BeginTransaction();
                try
                {
                    CLabelList clone = Clone(target, tx); //, parentId);
                    tx.Commit();
                    return clone;
                }
                catch
                {
                    tx.Rollback();
                    throw;
                }
            }
        }
        public CLabelList Clone(CDataSrc target, IDbTransaction txOrNull) //, int parentId)
        {
            CLabelList list = new CLabelList(this.Count);
            foreach (CLabel i in this)
                list.Add(i.Clone(target, txOrNull)); //, parentId));  *Child entities must reference the new parent
            return list;
        }
        #endregion
        
        #region Export to Csv
        //Web - Need to add a project reference to System.Web, or comment out these two methods
        public void ExportToCsv(HttpResponse response) { ExportToCsv(response, "Labels.csv"); }
        public void ExportToCsv(HttpResponse response, string fileName)
        {
            CDataSrc.ExportToCsv(response, fileName); //Standard response headers
            StreamWriter sw = new StreamWriter(response.OutputStream);
            ExportToCsv(sw);
            sw.Flush();
            response.End();
        }

        //Non-web
        public void ExportToCsv(string filePath)
        {
            StreamWriter sw = new StreamWriter(filePath);
            ExportToCsv(sw);
            sw.Close();
        }

        //Logic
        protected void ExportToCsv(StreamWriter sw)
        {
            string[] headings = new string[] {"LabelGuid", "LabelTagId"};
            CDataSrc.ExportToCsv(headings, sw);
            foreach (CLabel i in this)
            {
                object[] data = new object[] {i.LabelGuid, i.LabelTagId};
                CDataSrc.ExportToCsv(data, sw);
            }
        }
		#endregion



		public new void Add(CLabel item)
		{
			if (null != _index)
			{
				string k = Key(item.LabelGuid, item.LabelTagId);
				if (!_index.ContainsKey(k))
					_index[k] = item;
			}

			_indexByGuid = null;
			_indexByTagId = null;

			base.Add(item);
		}
		public new void Remove(CLabel item)
		{
			if (null != _index)
			{
				string k = Key(item.LabelGuid, item.LabelTagId);
				if (_index.ContainsKey(k))
					_index.Remove(k);
			}

			_indexByGuid = null;
			_indexByTagId = null;

			base.Remove(item);
		}
	}
}
